package com.xierong.remoteplayerlauncher.utils;

import android.util.Xml;

import com.xierong.remoteplayerlauncher.model.PlayListItem;

import org.xmlpull.v1.XmlPullParser;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

public class PlayerManager {
    public static String TAG = "RemotePlayerTag";
    private static PlayerManager instance = null;
    private PlayListItem currentPlaying = null;
    public int currentPlayIndex = 0;

    public boolean isLoop() {
        return isLoop;
    }

    public void setLoop(boolean loop) {
        isLoop = loop;
    }

    private boolean isLoop = true;

    public ArrayList<PlayListItem> getPlaylist() {
        return playlist;
    }

    public void setPlaylist(ArrayList<PlayListItem> playlist) {
        this.playlist = playlist;
        this.setCurrentPlaying(this.playlist.get(this.currentPlayIndex));
    }

    public PlayListItem restart() {
        this.currentPlayIndex = 0;
        this.currentPlaying = this.playlist.get(this.currentPlayIndex);
        return currentPlaying;
    }

    public PlayListItem playNext() {
        this.currentPlayIndex++;
        if(this.currentPlayIndex>=playlist.size()) {
            if (this.isLoop) {
                this.currentPlayIndex = 0;
            } else {
                this.currentPlayIndex = playlist.size() - 1;
            }
        }
        this.currentPlaying = this.playlist.get(this.currentPlayIndex);
        return currentPlaying;
    }
    public PlayListItem playPrev() {
        this.currentPlayIndex--;
        if(this.currentPlayIndex<0) {
            if (this.isLoop) {
                this.currentPlayIndex = playlist.size() - 1;
            } else {
                this.currentPlayIndex = 0;
            }
        }
        this.currentPlaying = this.playlist.get(this.currentPlayIndex);
        return currentPlaying;
    }

    public void setLoop() {
        if (playlist.size()<=1) {
            this.isLoop = false;
        } else {
            this.isLoop = !this.isLoop;
        }
    }

    private ArrayList<PlayListItem> playlist = new ArrayList<PlayListItem>();

    public PlayListItem getCurrentPlaying() {
        return currentPlaying;
    }

    public void setCurrentPlaying(PlayListItem currentPlaying) {
        this.currentPlaying = currentPlaying;
    }

    public ArrayList<PlayListItem> pathToPlaylist(String path) {
        try {
            File xmlFile = new File(path);
            FileInputStream fis = new FileInputStream(xmlFile);
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(fis, "utf-8");
            int eventType = parser.getEventType();
            ArrayList<PlayListItem> playlist = null;
            while(eventType != XmlPullParser.END_DOCUMENT) {
                PlayListItem item = new PlayListItem();
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        playlist = new ArrayList<PlayListItem>();
                        break;
                    case XmlPullParser.START_TAG:
                        if("media".equals(parser.getName())) {
                            item.setType(parser.getAttributeValue(null, "type"));
                            item.setDuration(parser.getAttributeValue(null, "duration"));
                            item.setPath(xmlFile.getParent().concat("/".concat(parser.getAttributeValue(null, "path"))));
                            item.setName(parser.nextText());
                            playlist.add(item);
                        }
                        break;
                    default:
                        break;
                }
                eventType = parser.next();
            }
            if(playlist != null && playlist.size()>0) {
                if (playlist.size()<=1) {
                    playlist.get(0).setFirst(true);
                    playlist.get(0).setLast(true);
                    this.setLoop(false);
                } else {
                    playlist.get(0).setFirst(true);
                    playlist.get(playlist.size()-1).setLast(true);
                }
                return playlist;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private PlayerManager() {}
    public static PlayerManager getInstance() {
        if (instance == null) {
            synchronized (PlayerManager.class) {
                if (instance == null) {
                    instance = new PlayerManager();
                }
            }
        }
        return instance;
    }
    public static String bytes2HexString(byte[] array) {
        StringBuilder builder = new StringBuilder();

        for (byte b : array) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            builder.append(hex);
        }

        return builder.toString().toUpperCase();
    }
}
